/*
 * BangoLogger.h
 *
 *  Created on: 24 Dec 2012
 *      Author: sonya
 */

#ifndef BANGOLOGGER_H_
#define BANGOLOGGER_H_

#include <bb/data/JsonDataAccess>

#include "BangoFieldNames.h"
#include "BangoConstants.h"
#include "BangoStatic.h"
#include "BangoSessionEvents.h"
#include "BangoNameValuePair.h"
#include "BangoProfile.h"
#include "BangoEvent.h"
#include "BangoStorage.h"

#include <QtLocationSubset/QGeoPositionInfo>
#include <QtLocationSubset/QGeoPositionInfoSource>
#include <QUrl>
#include <bb/device/CellularNetworkInfo>
#include <bb/device/SimCardInfo>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkConfiguration>
#include <QEventLoop>
#include <QDateTime>
#include <QSysInfo>
#include <bb/device/HardwareInfo>
#include <bps/deviceinfo.h>
#include <QMutex>
#include <bb/PackageInfo>
#include <QtCore>

class BangoConstants;
class BangoProfile;
class BangoStorage;


using namespace QtMobilitySubset;
using namespace bb::device;
using namespace bb::data;
using namespace bb::cascades;

class BangoLogger : public QObject
{
	Q_OBJECT

private:
	QString _applicationId;
	QString _userAgent;
	QString _userId;
	bool _isEndSession;
	BangoProfile _bangoProfile;
	BangoStorage _bangoStorage;
	QVector<BangoEvent> _bangoEvents;
	QVariant _events;
	bool _logEvents;
	int _sessionResumePeriod;
	QString _postURL;
	bool _useOfflineStorage;
	bool _findLocation;
	QString _locationSource;
	QString _applicationType;
	QString _applicationChannel;
	QString _applicationVersion;
	bool _persistDatabaseConnection;
	bool _isRoaming;
	int _maximumEventsPerProfile;
	bool _postDelayed;
	QString _jsonString;
	QString _networkName;
	QString _networkCountry;
	QString _networkId;

	QMutex postCurrentMutex;
	QMutex postSavedMutex;
	QMutex saveCurrentMutex;

	int LogInternalError(exception &ex, int errorLevel);
	void ClearEvents();
	void GetLoggerProperties();
	QPointer<QGeoPositionInfoSource> _locationDataSource;

	QGeoPositionInfo GetLocation();
	void setQVariantData(const QVariant &data);

	bool PostEvents(BangoProfile bangoProfile, QVector<BangoEvent> appEvents);
	QNetworkAccessManager *qnam;

public:
	BangoLogger(QObject *parent = 0);
	~BangoLogger();

	bool IsEndSession(){return _isEndSession;}
	void IsEndSession(bool isEndSession){_isEndSession = isEndSession;}
	void LogEvents(bool logEvents){ _logEvents = logEvents;}
	bool LogEvents(){return _logEvents;}
	void SessionResumePeriod(int sessionResumePeriod){_sessionResumePeriod = sessionResumePeriod; }
	int SessionResumePeriod(){ return _sessionResumePeriod; }
	void ApplicationType(QString applicationType){_applicationType = applicationType; }
	QString ApplicationType(){ return _applicationType; }
	void ApplicationVersion(QString applicationVersion){_applicationVersion = applicationVersion; }
	QString ApplicationVersion(){ return _applicationVersion; }
	void ApplicationChannel(QString applicationChannel){_applicationChannel = applicationChannel; }
	QString ApplicationChannel(){ return _applicationChannel; }
	void UserID(QString userID){_userId = userID;}
	QString UserID(){return _userId; }
	void UserAgent(QString userAgent){_userAgent = userAgent;}
	QString UserAgent(){return _userAgent; }
	void PostURL(QString postURL){_postURL = postURL;}
	QString PostURL(){return _postURL;}
	void UseOfflineStorage(bool useOfflineStorage){_useOfflineStorage = useOfflineStorage;}
	bool UseOfflineStorage(){return _useOfflineStorage;}
	void PersistDatabaseConnection(bool persistDatabaseConnection){_persistDatabaseConnection = persistDatabaseConnection;}
	bool PersistDatabaseConnection(){return _persistDatabaseConnection;}
	void FindLocation(bool findLocation){_findLocation = findLocation;}
	bool FindLocation(){return _findLocation;}
	void LocationSource(QString locationSource){_locationSource = locationSource;}
	QString LocationSource(){return _locationSource;}
	bool IsRoaming(){return _isRoaming;}
	void IsRoaming(bool isRoaming){_isRoaming = isRoaming;}
	int MaximumEventsPerProfile(){return _maximumEventsPerProfile;}
	void MaximumEventsPerProfile(int maximumEventsPerProfile){ if (maximumEventsPerProfile > 0) { _maximumEventsPerProfile = maximumEventsPerProfile;}}
	void PostDelayed(bool postDelayed){_postDelayed = postDelayed;}
	bool PostDelayed(){return _postDelayed;}

	void JSONString(QString jsonString){_jsonString = jsonString;}
	QString JSONString(){return _jsonString;}
	QString jsonData() const;
	void setJsonData (const QString &data);
	void writeJson(bool notCurrentProfile);
	void InitLogger(QString applicationID);
	int LogEvent(QString eventName, int errorLevel, QString eventValue, QString eventDetail, QString errorId, QMap<QString,QString> eventParameters);
	int LogEvent(QString eventName, QString eventValue, QString eventDetail, QMap<QString,QString> eventParameters);
	int LogEvent(QString eventName, QString eventValue, QString eventDetail);
	int LogEventStart(QString eventValue, QString eventDetail, QMap<QString,QString> eventParameters);
	int LogEventEnd(QString eventValue, QString eventDetail, QMap<QString,QString> eventParameters);
	int LogEventError(QString errorId, QString message, QString errorClass, QMap<QString,QString> eventParameters);
	int LogInternalErrorCritical(exception& ex);
	int LogInternalErrorWarning(exception& ex);
	QString GetApplicationVersion();
	 QString ViewBatch();
	 QString GetJSONString(BangoProfile bangoProfile, QVector<BangoEvent> bangoEvents);

	bool PostEvents(bool notCurrentProfile, bool closeDatabase);
	void PostStoredEvents();
	void PostCurrentEvents();
	void EndLocationUpdates();
	void CloseDatabase();



public slots:
	void positionUpdated(QGeoPositionInfo geoPositionInfo);
	void onReplyFinished();

};






#endif /* BANGOLOGGER_H_ */
